package pe.gob.mpfn.model;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table(name = "proteccion")
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "proteccion")
@RooDbManaged(automaticallyDelete = true)
public class Proteccion {

	@OneToMany(mappedBy = "intProteccionId")
    private Set<DesarrolloProteccion> desarrolloProteccions;

	@ManyToOne
    @JoinColumn(name = "int_tipo_proteccion_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intTipoProteccionId;

	@ManyToOne
    @JoinColumn(name = "int_dependencia_origen_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intDependenciaOrigenId;

	@ManyToOne
    @JoinColumn(name = "int_dependencia_destino_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intDependenciaDestinoId;

	@ManyToOne
    @JoinColumn(name = "int_parte_id", referencedColumnName = "int_parte_id", nullable = false)
    private Parte intParteId;

	@Column(name = "dat_fecha_inicio", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date datFechaInicio;

	@Column(name = "dat_fecha_fin", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date datFechaFin;

	@Column(name = "str_observaciones", columnDefinition = "VARCHAR", length = 200)
    @NotNull
    private String strObservaciones;

	@Column(name = "chr_estado", columnDefinition = "CHAR")
    @NotNull
    private Character chrEstado;

	public Set<DesarrolloProteccion> getDesarrolloProteccions() {
        return desarrolloProteccions;
    }

	public void setDesarrolloProteccions(Set<DesarrolloProteccion> desarrolloProteccions) {
        this.desarrolloProteccions = desarrolloProteccions;
    }

	public TablaMaestra getIntTipoProteccionId() {
        return intTipoProteccionId;
    }

	public void setIntTipoProteccionId(TablaMaestra intTipoProteccionId) {
        this.intTipoProteccionId = intTipoProteccionId;
    }

	public TablaMaestra getIntDependenciaOrigenId() {
        return intDependenciaOrigenId;
    }

	public void setIntDependenciaOrigenId(TablaMaestra intDependenciaOrigenId) {
        this.intDependenciaOrigenId = intDependenciaOrigenId;
    }

	public TablaMaestra getIntDependenciaDestinoId() {
        return intDependenciaDestinoId;
    }

	public void setIntDependenciaDestinoId(TablaMaestra intDependenciaDestinoId) {
        this.intDependenciaDestinoId = intDependenciaDestinoId;
    }

	public Parte getIntParteId() {
        return intParteId;
    }

	public void setIntParteId(Parte intParteId) {
        this.intParteId = intParteId;
    }

	public Date getDatFechaInicio() {
        return datFechaInicio;
    }

	public void setDatFechaInicio(Date datFechaInicio) {
        this.datFechaInicio = datFechaInicio;
    }

	public Date getDatFechaFin() {
        return datFechaFin;
    }

	public void setDatFechaFin(Date datFechaFin) {
        this.datFechaFin = datFechaFin;
    }

	public String getStrObservaciones() {
        return strObservaciones;
    }

	public void setStrObservaciones(String strObservaciones) {
        this.strObservaciones = strObservaciones;
    }

	public Character getChrEstado() {
        return chrEstado;
    }

	public void setChrEstado(Character chrEstado) {
        this.chrEstado = chrEstado;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "int_proteccion_id", columnDefinition = "INT")
    private Integer intProteccionId;

	public Integer getIntProteccionId() {
        return this.intProteccionId;
    }

	public void setIntProteccionId(Integer id) {
        this.intProteccionId = id;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Proteccion().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countProteccions() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Proteccion o", Long.class).getSingleResult();
    }

	public static List<Proteccion> findAllProteccions() {
        return entityManager().createQuery("SELECT o FROM Proteccion o", Proteccion.class).getResultList();
    }

	public static Proteccion findProteccion(Integer intProteccionId) {
        if (intProteccionId == null) return null;
        return entityManager().find(Proteccion.class, intProteccionId);
    }

	public static List<Proteccion> findProteccionEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Proteccion o", Proteccion.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Proteccion attached = Proteccion.findProteccion(this.intProteccionId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Proteccion merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Proteccion merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
